<?php
/* Project iSite
*  Author: iHao
*  File: /core/ajax.php
*  Usage: Loader
*/

if (ISITE == 1) {
	/* most important thing */
	require('settings.php');
} else {
	die('<strong>Project iSite</strong>');
}

class AJAXServer {
	var $db;

	public function __construct() {
		header('Content-type: text/html; charset=utf-8');
		header('Cache-control: no-cache, must-revalidate');
		$this->db = mysql_connect(ISITE_DB_HOSTNAME . ':' . ISITE_DB_PORT, ISITE_DB_USERNAME, ISITE_DB_PASSWORD);
		mysql_select_db(ISITE_DB_SCHEMATA);
		mysql_query("SET NAMES utf8");
		mysql_query("SET CHARACTER SET utf8");
		mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");
	}
	
	public function __destruct() {
		mysql_close($this->db);
	}

	public function ioList($page) {
		$o='[';
		$i = 1;
		$sql = "SELECT * FROM comments ORDER BY c_time desc LIMIT {$page},10";
		$rs = mysql_query($sql, $this->db);
		$num = mysql_num_rows($rs);
		//mysql_fetch_object
		while ($Com = mysql_fetch_assoc($rs)) {
			if ($i < $num) { $c = ','; } else { $c = ''; }
			$o = $o . json_encode($Com) . $c;
			$i++;
		}
		$o = $o . ']';
		mysql_free_result($rs);
		$this->io(1, $o);
	}

	public function ioDenied() {
		$this->ioXML();
		$this->ioMessage(999, 'Access denied');
	}

	private function io($code, $content) {
		$t = time();
		$prefix = 'ISITE AJAX Server';
		echo("{'generater':'{$prefix}','code':{$code},'time':'{$t}','contents':{$content}}");
	}
}
?>
